#!/usr/bin/env python
#coding:utf-8
import wx

class UrlInfoFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None,
                          -1, 'UrlInfo Frame',
                          size=(480, 400))
        panel = wx.Panel(self, -1)
        sizer = wx.GridSizer(rows=3,
                             cols=3,
                             hgap=5,
                             vgap=5)
        urlText = wx.StaticText(panel, -1, 'Url Enter:')
        self.urlInput = wx.TextCtrl(panel, -1, '', size=(320, -1))
        okButton = wx.Button(panel, wx.ID_OK, 'OK')
        t = wx.StaticText(panel, -1, 'Info:')
        self.infoText = wx.TextCtrl(panel, -1,
                                    size=(320, 300),
                                    style=wx.TE_MULTILINE)
        cleanButton = wx.Button(panel, wx.ID_CANCEL, 'Clean')
        #exitButton = wx.Button(panel, -1, 'Exit')

        sizer.Add(urlText, 0, 0)
        sizer.Add(self.urlInput, 0, 0)
        sizer.Add(okButton, 0, 0)
        sizer.Add(t, 0, 0)
        sizer.Add(self.infoText, 0, 0)
        sizer.Add(cleanButton, 0, 0)

        self.SetSizer(sizer)
        self.Fit()

        
        self.Bind(wx.EVT_BUTTON, self.OnButton, okButton)
        self.Bind(wx.EVT_BUTTON, self.OnClean, cleanButton)
        #self.Bind(wx.EVT_BUTTON, self.OnExit, exitButton)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
        
    def OnButton(self, event):
        url = self.urlInput.GetValue()
        info = self.geturlinfo(url)
        self.infoText.AppendText(info)

    def OnClean(self, event):
        self.infoText.Clear()

    def geturlinfo(self, url):
        sock = urllib2.urlopen(url)
        info = sock.info()
        return str(info) + '\n' + '-'*20

    #def OnExit(self, event):
    #    self.Close(True)

    def OnCloseWindow(self, event):
        self.Destroy()
    

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = UrlInfoFrame()
    frame.Show()
    app.MainLoop()
